#include "remote_Rx.h"
#include "usart.h"
void Remote_Angle_analysis(Remote_Rx_typedef *Remote_t);

Remote_Rx_typedef Remote_Rx=
{
	.ArrTE.header[0]=0xae,
	.ArrTE.End={0xff,0xfe},
	.button_Sum=8,
	.Rx_Position=0,
	.Rx_Flag=0,
	.Remote_safeNumber=10
};

/**
	* @brief 遥控串口初始化，用于初始化DMA空闲中断
  * @param Remote_Rx的结构体
  * @retval void
	*/
void Remote_UART_DMA_Init(Remote_Rx_typedef *Remote_t)
{
	HAL_UARTEx_ReceiveToIdle_DMA(&huart1,Remote_t->Rx_Arr,13);
}

/**
	* @brief 遥控串口初始化，用于初始化串口接收中断
  * @param Remote_Rx的结构体
  * @retval void
	*/
void Remote_UART_Receive_IT_Init(Remote_Rx_typedef *Remote_t)
{
	HAL_UART_Receive_IT(&huart1,&Remote_t->Rx_res,1);
}

/**
	* @brief 串口透传解析
  * @param Remote_Rx的结构体
  * @retval void
	*/
void Remote_UART_analysis(Remote_Rx_typedef *Remote_t,uint8_t res)
{
	if(Remote_t->Rx_Position==0 && res!=Remote_t->ArrTE.header[0])
	{
		Remote_t->Rx_Position=0;
		return;
	}
	
	switch(Remote_t->Rx_Position)
	{
		case 16:
			if(res!=Remote_t->ArrTE.End[0])
			{
				Remote_t->Rx_Position=0;
				return;
			}
			break;
			
		case 17:
			if(res!=Remote_t->ArrTE.End[1])
			{
				Remote_t->Rx_Position=0;
				return;
			}
			break;
	}
	
	if(Remote_t->Rx_Position<17)
	{
		Remote_t->Rx_Arr_Cache[Remote_t->Rx_Position]=res;
		Remote_t->Rx_Position++;
		return;
	}
	else
	{
		Remote_t->Rx_Arr_Cache[Remote_t->Rx_Position]=res;
		Remote_t->Rx_Position=0;
		Remote_t->Rx_Flag=1;
		
		Remote_RxArr_Analysis(Remote_t);
		
		//Remote_Angle_analysis(Remote_t);
		return;
	}
}

/**
	* @brief 遥控数据解析
  * @param Remote_Rx的结构体
  * @retval void
	*/
char dkzhhx[8]={0xbf,0xbb,0x00,0x00,0x00,0x00,0xdf,0xfd}; 
void Remote_RxArr_Analysis(Remote_Rx_typedef *Remote_t)
{
	uint8_t Check_t=0;
	
	//帧头帧尾判断
	if(
			!(Remote_t->Rx_Arr_Cache[0]==Remote_t->ArrTE.header[0])||
			!(Remote_t->Rx_Arr_Cache[16]==Remote_t->ArrTE.End[0])||
			!(Remote_t->Rx_Arr_Cache[17]==Remote_t->ArrTE.End[1])
		)return ;
	
	//校验位计算
	for(uint8_t i=0;i<15;i++)Check_t+=Remote_t->Rx_Arr_Cache[i];
	if(Check_t!=Remote_t->Rx_Arr_Cache[15])return ;
	
	//生命周期重置
	Remote_t->remote_tick=300;
	
	//数值转化
		Remote_t->Left_X=
			((0.01f*(((uint16_t)Remote_t->Rx_Arr_Cache[1]<<8)|Remote_t->Rx_Arr_Cache[2]))-50.0f)/50.0*100.0f;
		Remote_t->Left_Y=
			((0.01f*(((uint16_t)Remote_t->Rx_Arr_Cache[3]<<8)|Remote_t->Rx_Arr_Cache[4]))-50.0f)/50.0*100.0f;
		Remote_t->Right_X=
			((0.01f*(((uint16_t)Remote_t->Rx_Arr_Cache[5]<<8)|Remote_t->Rx_Arr_Cache[6]))-50.0f)/50.0*100.0f;
		Remote_t->Right_Y=
			((0.01f*(((uint16_t)Remote_t->Rx_Arr_Cache[7]<<8)|Remote_t->Rx_Arr_Cache[8]))-50.0f)/50.0*100.0f;
	
	//阈值限制
	if(fabs(Remote_t->Left_X)<Remote_t->Remote_safeNumber)Remote_t->Left_X=0;
	if(fabs(Remote_t->Left_Y)<Remote_t->Remote_safeNumber)Remote_t->Left_Y=0;
	if(fabs(Remote_t->Right_X)<Remote_t->Remote_safeNumber)Remote_t->Right_X=0;
	if(fabs(Remote_t->Right_Y)<Remote_t->Remote_safeNumber)Remote_t->Right_Y=0;
	
	//其他信息
	for(char i=0;i<4;i++)Remote_t->other_data[i]=Remote_t->Rx_Arr_Cache[i+11];
	
	//按键校验(防止错位bite)
	if(Remote_t->Rx_Arr_Cache[9]!=Remote_t->Rx_Arr_Cache[10])return ;
	
	//校验通过生成按键
	for(char i=0;i<4;i++)Remote_t->NoResButton[i]=(Remote_t->Rx_Arr_Cache[10]>>(i+4))&0x01;

	//复位按钮按下执行任务
	switch(Remote_t->Rx_Arr_Cache[10]&0x0f)
	{
		case 4:
			return;
		case 5:
			return;
		case 6:
			return;
		case 7:
			return;
		case 8:
			return;
		case 9:
			return;
		case 10:
			return;
		case 11:
			return;
		case 12:
			return;
		case 13:
			return;
		case 14:
			return;
		case 15:
			return;
	}
	
	//其他消息处理
	
}

/**
	* @brief 遥控角度解析
  * @param Remote_Rx的结构体
  * @retval void
	*/
void Remote_Angle_analysis(Remote_Rx_typedef *Remote_t)
{
	float distance_t;
	distance_t=sqrt((Remote_t->Left_X*Remote_t->Left_X)+(Remote_t->Left_Y*Remote_t->Left_Y));
	
	/*左摇杆角度求解*/
	if(distance_t==0)Remote_t->Left_Angle=0;
	else
	{
		//第一象限
		if(Remote_t->Left_X>=0&&Remote_t->Left_Y>=0)
			Remote_t->Left_Angle=acos(Remote_t->Left_Y/distance_t)*(180/3.1415926);
		//第二象限
		if(Remote_t->Left_X<0&&Remote_t->Left_Y>0)
			Remote_t->Left_Angle=360.0-acos(Remote_t->Left_Y/distance_t)*(180/3.1415926);
		//第三象限
		if(Remote_t->Left_X<0&&Remote_t->Left_Y<0)
			Remote_t->Left_Angle=180.0+acos(-Remote_t->Left_Y/distance_t)*(180/3.1415926);
		//第四象限
		if(Remote_t->Left_X>0&&Remote_t->Left_Y<0)
			Remote_t->Left_Angle=180.0-acos(-Remote_t->Left_Y/distance_t)*(180/3.1415926);
		//直线
		if(Remote_t->Left_X>0&&Remote_t->Left_Y==0)Remote_t->Left_Angle=90;
		//直线
		if(Remote_t->Left_X<0&&Remote_t->Left_Y==0)Remote_t->Left_Angle=270;
		//直线
		if(Remote_t->Left_X==0&&Remote_t->Left_Y>0)Remote_t->Left_Angle=0;
		//直线
		if(Remote_t->Left_X==0&&Remote_t->Left_Y<0)Remote_t->Left_Angle=180;
	}
	
	/*右摇杆角度求解*/
	distance_t=sqrt((Remote_t->Right_X*Remote_t->Right_X)+(Remote_t->Right_Y*Remote_t->Right_Y));
	if(distance_t==0)Remote_t->Right_Angle=0;
	else
	{
		//第一象限
		if(Remote_t->Right_X>=0&&Remote_t->Right_Y>=0)
			Remote_t->Right_Angle=acos(Remote_t->Right_Y/distance_t)*(180/3.1415926);
		//第二象限
		if(Remote_t->Right_X<0&&Remote_t->Right_Y>0)
			Remote_t->Right_Angle=360.0-acos(Remote_t->Right_Y/distance_t)*(180/3.1415926);
		//第三象限
		if(Remote_t->Right_X<0&&Remote_t->Right_Y<0)
			Remote_t->Right_Angle=180.0+acos(-Remote_t->Right_Y/distance_t)*(180/3.1415926);
		//第四象限
		if(Remote_t->Right_X>0&&Remote_t->Right_Y<0)
			Remote_t->Right_Angle=180.0-acos(-Remote_t->Right_Y/distance_t)*(180/3.1415926);
		//直线
		if(Remote_t->Right_X>0&&Remote_t->Right_Y==0)Remote_t->Right_Angle=90;
		//直线
		if(Remote_t->Right_X<0&&Remote_t->Right_Y==0)Remote_t->Right_Angle=270;
		//直线
		if(Remote_t->Right_X==0&&Remote_t->Right_Y>0)Remote_t->Right_Angle=0;
		//直线
		if(Remote_t->Right_X==0&&Remote_t->Right_Y<0)Remote_t->Right_Angle=180;
	}
}





